<template>
    <bi-table :data="list" dragsort @dragsort="dragsort" item-key="param_id" :loading="loading" height="300">
        <bi-table-column name="name" label="Name" width="100"></bi-table-column>
        <bi-table-column name="title" label="标题" ></bi-table-column>
        <bi-table-column name="type_title" label="类型" width="150"></bi-table-column>
        <bi-table-column  label="管理"  width="150"   v-slot="{row}">
            <bi-button type="primary" @click="handleEdit(row)"  >编辑</bi-button>
            <bi-button type="warning" @click="handleDel(row)" >删除</bi-button>
        </bi-table-column>
    </bi-table>
</template>

<script>
import paramEditDialog from 'param-edit-dialog.vue';

export default {
    dialog:{
        title:'参数列表',
        width:600,
        okButton:{
            text:'添加参数',
            icon:'fa-plus'
        }
    },
    data(){
        return {
            list:[],
            loading:false,
            model:{}
        }
    },
    props:{
        model_id:{
            type:[Number,String],
            default:''
        }
    },
    methods:{
        dialogOk(){
            this.handleEdit();
            return false;
        },
        handleDel(item){
            this.$confirm('确定要删除吗',(re)=>{
                this.$ajax({
                    url:this.$url({app:'web',controller:'admin.model',action:'delParam'}),
                    data:{
                        model_id:this.model_id,
                        param_id:item.param_id
                    },
                    success:(data)=>{
                        this.refresh();
                        this.dialog.callback();
                    }
                });
            });
        },
        handleEdit(item){

            let title =  item ? '编辑参数' : '添加参数';
            
            this.$dialog({
                type:'vue',
                title,
                content:admin.createApp(paramEditDialog,{item,model_id:this.model_id}),
                callback:(value)=>{
                    this.refresh();
                    this.dialog.callback();
                }
            });

        },
        refresh(){
            this.loading = true;
            this.$ajax({
                url:this.$url({app:'web',controller:'admin.model',action:'paramList'}),
                data:{
                    model_id:this.model_id
                },
                success:(data)=>{
                    this.list = data;
                    this.loading = false;
                }
            });
        },
        dragsort(value){
            this.$loading();

            this.$ajax({
                url:this.$url({app:'web',controller:'admin.model',action:'paramSort'}),
                data:{
                    model_id:this.model_id,
                    ids:value
                },
                success:(data)=>{
                    this.$loading(false)
                }
            });

        },
        clearCache(){

        }
    },
    mounted(){
        this.refresh();
    }
}
</script>